package com.bainuo.doctor.common.base;

import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bainuo.doctor.common.R;
import com.blankj.utilcode.utils.Utils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by faraway on 9/8/17.
 */


public abstract class EmptyBaseAdapter<T,K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter {



    /**
     * 空数据状态
     * 有数据状态
     */
    public static final int STATUS_EMPTY_DATA = 1;
    public static final int STATUS_HAS_DATA = 2;

    /**
     * 有数据与空数据的viewType
     */
    public static final int VIEW_TYPE_EMPTY = 1;
    public static final int VIEW_TYPE_DATA = 2;
    public static final int VIEW_TYPE_HEADEVIEW = 3;

    /**
     * 数据状态
     */
    private int mStatus = STATUS_HAS_DATA;


    private int res;
    protected int emptyIcon;
    protected String emptyStr;
    protected String emptyBtn;



    private Class<K> holderClass;
    protected List<T> mInfos = new ArrayList();

    public EmptyBaseAdapter(List<T> infos,Class<K> holderClass,int res){
        this.res = res;
        this.holderClass = holderClass;
        mInfos = infos;
    }

    protected EmptyBaseAdapter() {
    }

    /**
     * 设置状态
     *
     * @param status status
     */
    public void setStatus(int status) {
        mStatus = status;
    }

    /**
     * 获取当前状态
     *
     * @return
     */
    public int getStatus() {
        return mStatus;
    }

    @Override
    public int getItemViewType(int position) {
        if (getHeaderView() != null) {
            if (position == 0) {
                return VIEW_TYPE_HEADEVIEW;
            }
        }
        if (getStatus() == STATUS_EMPTY_DATA) {
            return VIEW_TYPE_EMPTY;
        }
        return VIEW_TYPE_DATA;
//
//        if (mInfos == null || mInfos.size() == 0){
//            return VIEW_TYPE_EMPTY;
//        }else {
//            return VIEW_TYPE_DATA;
//        }
    }


    private View mHeaderView;
    public void setHeaderView(View view){
        mHeaderView = view;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_EMPTY) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_view, parent, false);
            return new EmptyViewHolder(view);
        }
        if (viewType == VIEW_TYPE_HEADEVIEW) {
            return getHeaderViewHolder(parent, viewType);
        }

        try {
            View view = LayoutInflater.from(parent.getContext()).inflate(res, parent, false);
            Constructor constructors[] = holderClass.getConstructors();
            for (int i=0;i<constructors.length;i++){
                Class[] paramTypes = constructors[i].getParameterTypes();
                if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(View.class)){
                    return (RecyclerView.ViewHolder)constructors[i].newInstance(view);
                }else if (paramTypes.length == 2 && paramTypes[1].isAssignableFrom(View.class)){
                    return (RecyclerView.ViewHolder)constructors[i].newInstance(this,view);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        if (holder instanceof EmptyBaseAdapter.EmptyViewHolder) {
            EmptyViewHolder viewHolder = (EmptyViewHolder) holder;
            //默认操作
            viewHolder.tvEmptyView.setText(emptyStr);
            onEmptyViewHolder(viewHolder);
            viewHolder.tvEmptyView.setVisibility (emptyStr ==null?View.GONE:View.VISIBLE);
            return;
        }

        if (holderClass.isInstance(holder)){
            if(getHeaderView() != null)
                position -= 1;

            onHandlerViewHolder((K)holder, position);
            return;
        }
        if (position == 0){
            onHandlerHeaderViewHolder(holder);
        }

    }

    /**
     * 处理viewHolder
     *
     * @param holder   holder
     * @param position position
     */
    protected abstract void onHandlerViewHolder(K holder, int position);

    protected void onHandlerHeaderViewHolder(RecyclerView.ViewHolder holder){};


    /**
     * 获取空数据显示描述
     *
     * @return return
     */
//    protected CharSequence getEmptyStatusText(){return emptyStr;};


    /**
     * 获取数据count
     *
     * @return return
     */
    public int getDataItemCount(){return mInfos==null?0:mInfos.size();};


    /**
     * 获取headerView
     *
     * @return
     */
    protected View getHeaderView(){return mHeaderView;};

    /**
     * 获取headerView viewHolder
     *
     * @param parent   parent
     * @param viewType viewType
     * @return return  return
     */
    protected RecyclerView.ViewHolder getHeaderViewHolder(ViewGroup parent, int viewType){
        View v = getHeaderView();
        if (v != null){
            return new RecyclerView.ViewHolder(v) {
            };
        }
        return null;
    };

    /**
     * 空视图处理
     *
     * @param viewHolder viewHolder
     */
    protected void onEmptyViewHolder(EmptyViewHolder viewHolder) {

        if (emptyIcon != 0){
            Drawable drawable = ResourcesCompat.getDrawable(Utils.getContext().getResources(), emptyIcon, null);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.tvEmptyView.setCompoundDrawables(null,drawable,null,null);
        }

        if (emptyBtn != null){
            viewHolder.layoutBtn.setVisibility(View.VISIBLE);
            viewHolder.btn.setText(emptyBtn);
        }
    }


    @Override
    public int getItemCount() {
        if (getHeaderView() == null) {
            if (getStatus() == STATUS_HAS_DATA) {
                return getDataItemCount();
            } else {
                return 1;
            }
        } else {
            //在headerView之后显示emptyView
            if (getStatus() == STATUS_HAS_DATA) {
                return getDataItemCount() + 1;
            } else {
                return 2;
            }
        }
    }

//    void notifyDataSetChanged(){
//        if (mList.size() > 0) {
//            mAdapter.setStatus(STATUS_HAS_DATA);
//        } else {
//            mAdapter.setStatus(STATUS_EMPTY_DATA);
//        }
//    }



    /**
     * 空emptyViewHolder
     */
    public static class EmptyViewHolder extends RecyclerView.ViewHolder {

//        @BindView(R.id.tv_empty)
        public TextView tvEmptyView;
//        @BindView(R.id.layout_btn)
        public FrameLayout layoutBtn;
//        @BindView(R.id.btn_create)
        public TextView btn;

        public EmptyViewHolder(View itemView) {
            super(itemView);
//            ButterKnife.bind(this, itemView);
            tvEmptyView = (TextView) itemView.findViewById(R.id.tv_empty);
            layoutBtn = (FrameLayout) itemView.findViewById(R.id.layout_btn);
            btn = (TextView) itemView.findViewById(R.id.btn_create);
        }
    }

    public void setEmptyInfo(AdapterEmptyInfo info){
        if (info != null){
            emptyBtn = info.btn;
            emptyIcon = info.icon;
            emptyStr = info.note;
        }
    }

    public static class AdapterEmptyInfo{
        public int icon;
        public String note;
        public String btn;

        public AdapterEmptyInfo(int icon, String note, String btn) {
            this.icon = icon;
            this.note = note;
            this.btn = btn;
        }
    }

}
